#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 BidiX (http://bidix.info)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
'''
	notewikiapp_client - HTTP Client to Notewiki App
	
	Description :
		- Issue request to a Notewiki App
		- use gae_client.py to issue request to Google App Engine 
	
	
 
	Usage :
		notewikiapp_client.py {method} {article_name}
		
		Use notewikiapp_client.config to specify environment of NotewikiApp
	 
'''
import sys
from ConfigParser import ConfigParser
from urllib import urlencode
from gae_client import GAE_Client

class Notewikiapp_Client(GAE_Client):
	'''
		
	'''
	config_filename = 'notewikiapp_client.config'
	server_root_url = '/'
	
	def __init__(self, notewikiapp):
		config = ConfigParser()
		config.read(self.config_filename)
		self.app = notewikiapp
		self.domain = config.get(notewikiapp, 'domain')
		self.user = config.get(notewikiapp, 'user')
		self.password = config.get(notewikiapp, 'password')
		self.cookies_filename = config.get(notewikiapp, 'cookies_filename')
		GAE_Client.__init__(self, self.user, self.password, self.domain, self.app, self.cookies_filename, local = False)
		
	def post_article(self, name, title, text, tag):
		url = self.server_root_url+name
		body = urlencode({'name': name, 'title': title, 'text': text, 'tag': tag})
		return self.request('POST', url, body)

def main(argv):
	app = argv[1]
	method = argv[2]
	url = argv[3]
	data = None
	if len(argv) > 4:
		data = argv[3]
	app = Notewikiapp_Client(app)
	code, body = app.request(method, url, data)
	if code == 200:
		print body
	else:
		print "Return code : %s"%code
		print "body : \n%s"%body

if __name__ == '__main__':
    main(sys.argv)
